<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class page_model extends CI_Model {
	private $table = 'jonu_pages';

	public function count_all(){
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	public function count_published(){
		$this->db->where('jonu_pages_status', "Published");
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	public function count_drafted(){
		$this->db->where('jonu_pages_status', "Drafted");
		$query = $this->db->count_all_results($this->table);
		return $query;
	}
	
	public function get_published(){
		$this->db->join('jonu_admin', 'jonu_admin.jonu_admin_id = jonu_pages.jonu_admin_id');
		$this->db->where('jonu_pages_status', 'Published');
		$query = $this->db->get($this->table);
		$result = $query->result_array();
		return $result;
	}

	public function published_paged($num, $offset){
		$this->db->join('jonu_admin', 'jonu_admin.jonu_admin_id = jonu_pages.jonu_admin_id');
		$this->db->where('jonu_pages_status', 'Published');
		$this->db->order_by('jonu_pages_id', 'desc');		
		$query = $this->db->get($this->table, $num, $offset);
		$result = $query->result_array();
		return $result;
	}

	public function get_drafted(){
		$this->db->join('jonu_admin', 'jonu_admin.jonu_admin_id = jonu_pages.jonu_admin_id');
		$this->db->where('jonu_pages_status', 'Drafted');
		$query = $this->db->get($this->table);
		$result = $query->result_array();
		return $result;
	}

	public function store($param){
		$data = array(
			'jonu_pages_title'	=> $param['page_title'],
			'jonu_pages_content'	=> $param['page_content'],
			'jonu_pages_date'	=> date('Y-m-d m:s'),
			'jonu_pages_status'	=> $param['page_status'],
			'jonu_admin_id'	=> get_cookie('c_adm_id'),
			'jonu_pages_permalink'	=> strtolower($param['page_permalink'])
			);
		$query = $this->db->insert($this->table, $data);
		return $query;
	}

	public function get_by_id($id){
		$this->db->join('jonu_admin', 'jonu_admin.jonu_admin_id = jonu_pages.jonu_admin_id');
		$this->db->where('jonu_pages_id', $id);
		$query = $this->db->get($this->table);
		$result = $query->result_array();
		return $result;
	}

	public function get_by_permalink($permalink){
		$this->db->where('jonu_pages_status', "Published");
		$this->db->where('jonu_pages_permalink', $permalink);
		$query = $this->db->get($this->table);
		$result  = $query->result_array();

		return $result;
	}

	public function update($param){
		$data = array(
			'jonu_pages_title'	=> $param['page_title'],
			'jonu_pages_content'	=> $param['page_content'],
			'jonu_pages_status'	=> $param['page_status'],
			'jonu_pages_permalink'	=> strtolower($param['page_permalink'])
			);
		$this->db->where('jonu_pages_id', $param['page_id']);
		$query = $this->db->update($this->table, $data);
		return $query;
	}

	public function destroy($id){
		$this->db->where('jonu_pages_id', $id);
		$query = $this->db->delete($this->table);
		return $query;
	}
}

/* End of file page_model.php */
/* Location: ./application/models/page_model.php */
